<?php

include ("../../classes/Imagem.php");

$galeria = $_GET['galeria'];

if (count($_FILES['files'])) {
//    foreach ($_FILES['files']['name'] as $i => $name) {
//        $tmp_name = $_FILES['files']['tmp_name'][$i];
//        move_uploaded_file($tmp_name, 'files/' . $galeria . '/' . $name);
//    }

    $small_largura = 450;
    $small_altura = 300;

    $size = 1024;

    if (isset($_FILES['files'])) {
        foreach ($_FILES['files']['name'] as $i => $name) {
            $tmp_name = $_FILES['files']['tmp_name'][$i];
            if ($tmp_name != '') {
                $destinoImgBig = 'files/' . $galeria . '/' . md5($name) . '.jpg';
                move_uploaded_file($tmp_name, $destinoImgBig);

                $oImg = new m2brimagem($destinoImgBig);
                $oImg->redimensiona($small_largura, $small_altura, 'crop');
                $oImg->grava('files/' . $galeria . '/small/' . md5($name) . '.jpg');

                // Pega os tamanhos originais
                $infoimg = getimagesize($destinoImgBig);

                // Cria uma taxa proporcional para conversão
                $tax = $infoimg[0] / $infoimg[1];
                if ($tax < 1) {
                    $newWidth = round($size * $tax);
                    $newHeight = $size;
                } else {
                    $newWidth = $size;
                    $newHeight = round($size / $tax);
                }

                $oImgBig = new m2brimagem($destinoImgBig);
                $oImgBig->redimensiona($newWidth, $newHeight);
                $oImgBig->grava($destinoImgBig);
            }
        }
    }

    header('Location: http://' . $_SERVER['SERVER_NAME'] . '/admin/galeria/fotos/?galeria=' . $galeria);
} else {
    echo ("HTTP/1.0 400 Nenhuma foto selecionada.");
    return;
}
?>